package com.jwthhealth_pub.old.healthyscreening.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.healthyscreening.model.HealthyScreenModel;
import com.jwthhealth_pub.old.healthyscreening.view.adapter.HealthyScreeningAdapter;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 健康筛查页面
 * Created by Administrator on 2016/12/21.
 */
public class HealthyScreeningActivity extends Activity {
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    private HealthyScreeningAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthy_screening);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        initClick();
        initRecycleView();
    }

    private void initRecycleView() {
        adapter = new HealthyScreeningAdapter(this);
        recycleview.setAdapter(adapter);
        recycleview.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initClick() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void proressbar() {
        progressbar.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        List<Integer> answers = adapter.getAnswers();
        for (int i = 0; i < 20; i++) {
            if (answers.get(i) == -1) {
                Toast.makeText(this, getResources().getString(R.string.physicalfiness_alert), Toast.LENGTH_SHORT).show();
                return;
            }
        }
        String answer = "";
        for (int i = 0; i < 20; i++) {
            answer = answer + answers.get(i) + ",";
        }
        answer = answer.substring(0, answer.length() - 1);


        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        Call<HealthyScreenModel> healthyScreenModelCall = null;
        if (userinfoModel != null) {
            healthyScreenModelCall = ApiHelper.healthyScreenCommit(userinfoModel.getId(), answer, userinfoModel.getAndroidtoken());
        } else {
            healthyScreenModelCall = ApiHelper.healthyScreenCommit(null, answer, null);
        }

        healthyScreenModelCall.enqueue(new Callback<HealthyScreenModel>() {
            @Override
            public void onResponse(Call<HealthyScreenModel> call, Response<HealthyScreenModel> response) {
                HealthyScreenModel body = response.body();
                if (body.getCode().equals("0")) {
                    Intent intent = new Intent(HealthyScreeningActivity.this, HealthyscreeningAnswerActivity.class);
                    intent.putExtra(Constant.HEALTHYSCREENANSWER, body.getData().getResult());
                    startActivity(intent);
                    finish();
                } else {
                    Log.d("HealthyScreeningAdapter", "fail");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            proressbar();
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<HealthyScreenModel> call, Throwable t) {
                Log.d("HealthyScreeningAdapter", "error");
            }
        });
        proressbar();
    }
}
